#include "World.h"

static void count_dims(FILE* file_pointer,int* dimensions)
{
	int i = 0;
	int j = 0;
	while (!feof(file_pointer))
	{
		j = 0;
		while (fgetc(file_pointer) != '\n' && !feof(file_pointer))
		{
			dimensions[1] = ++j;
		}
		dimensions[0] = ++i;
	}
}

void create(struct World* world,char* file,int width,int height)
{
	FILE* file_pointer;
	int dimensions[2];
	int i;
	int j;
	char test;
	struct Tile current_tile;
	struct Tile* world_tiles;
	struct Car* world_cars;
	file_pointer = fopen(file,"r");
	count_dims(file_pointer,dimensions);
	world_tiles = (struct Tile *)malloc(dimensions[0] * dimensions[1] * sizeof(struct Tile));
	world_cars = (struct Car*)malloc(dimensions[0] * dimensions[1] / TILES_CARS_RATIO * sizeof(struct Car));
	rewind(file_pointer);
	i = 0;
	while(!feof(file_pointer))
	{
		char current_char = fgetc(file_pointer);
		if (current_char != '\n' && !feof(file_pointer))
		{
			current_tile.type = current_char;
			current_tile.cars[0] = NULL;
			current_tile.cars[1] = NULL;
			current_tile.ncar = 0;
			world_tiles[i++] = current_tile;
		}
	}
	fclose(file_pointer);
	world ->height = dimensions[0];
	world ->width = dimensions[1];
	world ->map = world_tiles;
	world ->cars = world_cars;
}

void draw(struct World* world)
{
	int i;
	int j;
	system("cls");
	for (i = 0; i < world ->height; i++)
	{
		for (j = 0; j < world ->width; j++)
		{
			char ncar = world ->map[i * world ->width + j].ncar;
			switch(ncar)
			{
				case 0: printf("%c",world ->map[i * world ->width + j].type); break;
				case 1: printf("1"); break;
				case 2: printf("2"); break;
			}
							
			if (j == world ->width - 1)
				printf("\n");
		}
	}
}

void get_tile(struct World* world,struct Tile* res_tile,int x,int y)
{
	if (x < world ->height && x < world ->width)
	{
		printf("Cohordinates out of the game world\n");
		exit(0);
	}
	else
		res_tile = &(world ->map[x * world ->width + y]);
}

static double randf_range(double a,double b)
{
	srand(time(0));
	return ((b - a) * ((double)rand() / RAND_MAX)) + a;
}

static enum Direction select_dir(struct Car* car,struct World* world)
{
	struct Tile* current_tile;
	get_tile(world,current_tile,car ->posx, car ->posy);
	if (current_tile ->type == '=')
	{
		struct Tile* neighbor;
		if (car ->direction == RIGHT)
		{
			get_tile(world,neighbor,car ->posx + 1,car ->posy);
			if (neighbor ->type != '*' && neighbor ->type != '|')
				return RIGHT;
			else
				return LEFT;
		}
		else if (car ->direction == LEFT)
		{
			get_tile(world,neighbor,car ->posx - 1,car ->posy);
			if (neighbor ->type != '*' && neighbor ->type != '|')
				return LEFT;
			else
				return RIGHT;
		}
	}
	else if (current_tile ->type == '|')
	{
		struct Tile* neighbor;
		if (car ->direction == UP)
		{
			get_tile(world,neighbor,car ->posx,car ->posy - 1);
			if (neighbor ->type != '*' && neighbor ->type != '=')
				return UP;
			else
				return DOWN;
		}
		else if (car ->direction == DOWN)
		{
			get_tile(world,neighbor,car ->posx,car ->posy + 1);
			if (neighbor ->type != '*' && neighbor ->type != '=')
				return DOWN;
			else
				return UP;
		}
	}
	else if (current_tile ->type == '+' || current_tile ->type == 'X')
	{
	}
}

void update_car(struct Car* car, struct World* world)
{
	int i;
	for (i = 0; i < car ->velocity; i++)
	{

	}
}